<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form class="-mb-15px" :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
      <el-form-item label="年级名称" prop="gradeName">
        <el-select v-model="queryParams.gradeName" placeholder="请选择年级名称" clearable class="!w-240px">
          <el-option v-for="dict in gradeList" :key="dict.gradeName" :label="dict.gradeName" :value="dict.gradeName" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级名称" prop="className">
        <el-select v-model="queryParams.className" placeholder="请选择班级名称" clearable class="!w-240px">
          <el-option v-for="dict in classList" :key="dict.className" :label="dict.className" :value="dict.className" />
        </el-select>
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input
v-model="queryParams.studentName" placeholder="请输入学生姓名" clearable @keyup.enter="handleQuery"
          class="!w-240px" />
      </el-form-item>

      <el-form-item label="请假状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择请假状态" clearable class="!w-240px">
          <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_LEAVE_APPROVAL_STATUS)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="请假类型 " prop="leaveType">
        <el-select v-model="queryParams.leaveType" placeholder="请选择请假类型" clearable class="!w-240px">
          <el-option
v-for="dict in getIntDictOptions(DICT_TYPE.SYSTEM_LEAVE_TYPE)" :key="dict.value"
            :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="开始时间" prop="leaveStartTime">
        <el-date-picker v-model="queryParams.leaveStartTime" value-format="YYYY-MM-DD HH:mm:ss" type="date" :default-time="new Date('1 00:00:00')"/>
      </el-form-item>
      <el-form-item label="结束时间" prop="leaveEndTime">
        <el-date-picker v-model="queryParams.leaveEndTime" value-format="YYYY-MM-DD HH:mm:ss" type="date" :default-time=" new Date('1 23:59:59')"/>
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" /> 搜索
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" /> 重置
        </el-button>
        <el-button
type="success" plain @click="handleExport" :loading="exportLoading"
          v-hasPermi="['system:student-leave:export']">
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="学校名称" align="center" prop="tenantName" />

      <el-table-column label="年级" align="center" prop="gradeName">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_CONFIG_TYPE" :value="scope.row.gradeName" />
        </template>
      </el-table-column>
      <el-table-column label="班级" align="center" prop="className" />
      <el-table-column label="学生姓名" align="center" prop="studentName" />
      <el-table-column label="请假类型" align="center" prop="leaveType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_LEAVE_TYPE" :value="scope.row.leaveType" />
        </template>
      </el-table-column>

      <el-table-column label="请假开始时间" align="center" prop="leaveStartTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="请假结束时间" align="center" prop="leaveEndTime" :formatter="dateFormatter" width="180px" />
      <el-table-column label="请假状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SYSTEM_LEAVE_APPROVAL_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
link type="primary" @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:student-leave:update']">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
:total="total" v-model:page="queryParams.pageNo" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <StudentLeaveForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { StudentLeaveApi, StudentLeaveVO } from '@/api/system/studentleave'
import StudentLeaveForm from './StudentLeaveForm.vue'
import { GradeApi, GradeVO } from '@/api/system/grade'
import { ClassesApi, ClassesVO } from '@/api/system/classes'

/** 学生请假 列表 */
defineOptions({ name: 'StudentLeave' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<StudentLeaveVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  studentName: undefined,
  gradeName: undefined,
  className: undefined,
  status: undefined,
  leaveStartTime: [],
  leaveEndTime: [],
  leaveType: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const gradeList = ref<GradeVO[]>([])  // 导出的加载中
const classList=ref<ClassesVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StudentLeaveApi.getStudentLeavePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}
const getgradeList = async () => {
  try {
    const data = await GradeApi.getGradelist({
      pageNo: 1,
      pageSize: 100,
    })
    gradeList.value= data.list
  } finally {
    loading.value = false
  }
}
const getclassesList = async () => {
  try {
    const data = await ClassesApi.getClasseslist({
      pageNo: 1,
      pageSize: 100,
    })
    classList.value = data.list
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */ 
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StudentLeaveApi.deleteStudentLeave(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await StudentLeaveApi.exportStudentLeave(queryParams)
    download.excel(data, '学生请假.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  // 获取年级数据
  getgradeList()
  // 获取班级数据
  getclassesList()

})
</script>